using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace Lib
{
    public class DataRetriever : IDataRetriever
    {
        protected const int InternalBufferSize = 4096;
        protected byte[] _internalBuffer;

        public DataRetriever() {
            _internalBuffer = new byte[InternalBufferSize];
        }

        IEnumerable<byte> IDataRetriever.Retrieve(string fullPath, long fileOffset, int length)
        {
            var returnValue = new List<byte>();
            int read;
            using(FileStream stream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                stream.Seek(fileOffset, SeekOrigin.Begin);
                while ((read = stream.Read(_internalBuffer, 0, InternalBufferSize)) > 0)
                    returnValue.AddRange(_internalBuffer.Take(read));
                stream.Close();
            }

            return returnValue;
        }
    }
}